package com.china08.yxyapi.api.manage.tec.teaching.lecture.pack;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.lecture.YxyLecturePack;
import com.china08.yxyapi.entity.mg.lecture.YxyLecturePrePack;
import com.china08.yxyapi.entity.mg.lecture.YxyLecturePrePack.Res;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.lecture.YxyLecturePackRepository;
import com.china08.yxyapi.repository.mg.lecture.YxyLecturePrePackRepository;

import io.swagger.annotations.ApiOperation;

/**
 * 老师 ---备授课--添加授课（发布备课包）
 * 
 */
@CrossOrigin
@RestController
public class LectureCrtPackController extends YxyApiBaseController {

    @Autowired
    private YxyLecturePrePackRepository yxyLecturePrePackRepository;

    @Autowired
    private YxyLecturePackRepository yxyLecturePackRepository;

    @ApiOperation(value = "老师 ---备授课--添加授课（发布备课包）")
    @RequestMapping(value = "/api_yw/manage/tec/teaching/lecture/pack/crt", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Object process(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel req,
            RespModel respModel) throws ParseException {

        String userId = getProfile(request, response).getId();
        String schoolId = getProfile(request, response).getSchoolId();

        Assert.notNull(StringUtils.defaultIfBlank(userId, null), "用户id必须");
        Assert.notNull(StringUtils.defaultIfBlank(schoolId, null), "学校id必须");

        YxyLecturePrePack yxyLecturePrePack = yxyLecturePrePackRepository.findById(req.prePackId);
        YxyLecturePack yxyLecturePack = null;

        if (yxyLecturePrePack != null) {
            for (MyClass classes : req.myClass) {
                yxyLecturePack = new YxyLecturePack();
                // BeanUtils.copyProperties(yxyLecturePack,
                // yxyLecturePrePack);
                String phaseId = yxyLecturePrePack.getPhaseId();
                String phaseName = yxyLecturePrePack.getPhaseName();
                String versionId = yxyLecturePrePack.getVersionId();
                String versionName = yxyLecturePrePack.getVersionName();
                String semesterId = yxyLecturePrePack.getSemesterId();
                String semesterName = yxyLecturePrePack.getSemesterName();
                String bookContentId = yxyLecturePrePack.getBookcontentsId();
                String bookContentName = yxyLecturePrePack.getBookcontentsName();
                String packName = yxyLecturePrePack.getPackName();
                yxyLecturePrePack.setUsedCount(yxyLecturePrePack.getUsedCount() + 1l);
                yxyLecturePrePackRepository.save(yxyLecturePrePack);
                List<Res> res = yxyLecturePrePack.getResources();
                List<com.china08.yxyapi.entity.mg.lecture.YxyLecturePack.Res> packRes = new ArrayList<YxyLecturePack.Res>();
                for (Res res2 : res) {
                    com.china08.yxyapi.entity.mg.lecture.YxyLecturePack.Res res3 = new com.china08.yxyapi.entity.mg.lecture.YxyLecturePack().new Res();
                    res3.setOrderNum(res2.getOrderNum());
                    res3.setCourseType(res2.getCourseType());
                    res3.setFormatId(res2.getFormatId());
                    res3.setResourceId(res2.getResourceId());
                    res3.setResourceName(res2.getResourceName());
                    res3.setResourceUrl(res2.getResourceUrl());
                    packRes.add(res3);
                }
                yxyLecturePack.setPhaseId(phaseId);
                yxyLecturePack.setPhaseName(phaseName);
                yxyLecturePack.setPackName(packName);
                yxyLecturePack.setVersionId(versionId);
                yxyLecturePack.setVersionName(versionName);
                yxyLecturePack.setSemesterId(semesterId);
                yxyLecturePack.setSemesterName(semesterName);
                yxyLecturePack.setBookcontentsId(bookContentId);
                yxyLecturePack.setBookcontentsName(bookContentName);
                yxyLecturePack.setResources(packRes);
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");
                req.preDate = req.preDate.replace("Z", " UTC");
                Date date = format.parse(req.preDate);
                yxyLecturePack.setPreDate(date);
                yxyLecturePack.setUserId(userId);
                yxyLecturePack.setUserNick(yxyLecturePrePack.getUserNick());
                yxyLecturePack.setSchoolNick(yxyLecturePrePack.getSchoolNick());
                yxyLecturePack.setSchoolId(schoolId);
                yxyLecturePack.setClassId(classes.classId);
                yxyLecturePack.setClassName(classes.classNick);
                yxyLecturePackRepository.save(yxyLecturePack);
            }
        }

        respModel = new RespModel();
        respModel.setMessage("备授课-成品备课包创建成功");
        return respModel;
    }

    protected static class ReqModel extends YxyApiReqModel {

        public String prePackId;

        public String preDate;
        /**
         * 资源id
         */
        public List<MyClass> myClass;

        @Override
        public void autoCheck() {
            Assert.notNull(StringUtils.defaultIfBlank(prePackId, null), "请选择授课目录");
            Assert.notNull(StringUtils.defaultIfBlank(preDate, null), "授课时间为必填项");
            Assert.notEmpty(myClass, "班级必须大于1个");
        }

    }

    protected static class MyClass {
        public String classId;
        public String classNick;

    }

    protected static class RespModel extends YxyApiRespModel {

    }
}
